{
  class Node {
    constructor(public element: any = null, public next: Node | null = null) {}
  }

  class LinkList<T> {
    public head = new Node(1);
    public length: number = 1;
    constructor() {
      this.head.next = this.head;
    }
    find(item: T): Node {
      let currNode = this.head;
      while (currNode.next && currNode.next.element !== 1) {
        if (currNode.element === item) {
          break;
        } else {
          currNode = currNode.next;
        }
      }
      return currNode;
    }
    findPrevious(item: T): Node {
      // 查找一个元素的前一个元素
      let currNode = this.head;
      while (currNode.next) {
        if (currNode.next.element === item) {
          break;
        } else {
          currNode = currNode.next;
        }
      }
      return currNode;
    }
    insert(newElement: T, item: T): void {
      let node = new Node(newElement);
      let currNode = this.find(item);
      node.next = currNode.next;
      currNode.next = node;
      this.length++;
    }
    remove(item: T): void {
      let prevNode = this.findPrevious(item);
      if (prevNode.next) {
        prevNode.next = prevNode.next.next;
      }
      this.length--;
    }
    display(): void {
      let currNode = this.head;
      while (currNode.next) {
        console.log(currNode.element);
        if (currNode.next && currNode.next.element !== 1) {
          currNode = currNode.next;
        } else {
          break;
        }
      }
    }
    // handleKill() {
    //   let currNode = this.head.next?.next?.element;
    //   // console.log(currNode)
    //   // while(currNode.next)
    // }
  }

  let list = new LinkList<number>();
  list.insert(2, 1);
  for (let i = 3; i <= 41; i++) {
    list.insert(i, i - 1);
  }

  const killOne = (list: LinkList<number>) => {
    let start = list.head;
    while (list.length >= 3) {
      if (list.head.next?.next) {
        let current = start.next?.next as Node;
        start = current?.next as Node;
        list.head = start;
        // console.log(current.element);
        list.remove(current.element);
      }
    }
    // console.log(list.head.element);
    // console.log(list.head.next.element);
  };

  killOne(list);
}
